﻿using AntJoin.Core.Configuration;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Text;
using IConfiguration = AntJoin.Core.Configuration.IConfiguration;

namespace AntJoin.Log
{
    /// <summary>
    /// 日志公共类
    /// </summary>
    public static class LogCommon
    {
        public static IConfiguration config = new ApolloConfiguration("core");

        /// <summary>
        /// Project名称
        /// </summary>
        public static readonly string ProjectName = "LogProjectName";
        /// <summary>
        /// Endpoint地址
        /// </summary>
        public static readonly string Endpoint = "LogEndpoint";
        /// <summary>
        /// 主日志库名称
        /// </summary>
        public static readonly string StoreName = "LogStoreName";
        /// <summary>
        /// 运营日志库名称
        /// </summary>
        public static readonly string OperationalStoreName = "LogOperationalStoreName";
        public static uint UnixTimeSpan(DateTime timestamp)
        {
            return (uint)((timestamp.Ticks - 621356256000000000) / 10000000);
        }

        public static DateTime ConvertUnixTimeSpan(uint time)
        {
            return new DateTime(621356256000000000 + (long)time * 10000000);
        }

        /// <summary>
        /// 将object转换为bool，若转换失败，则返回指定值。不抛出异常。  
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ToBool(object str, bool result)
        {
            try
            {
                return bool.Parse(str.ToString());
            }
            catch
            {
                return result;
            }
        }

    }
}
